package com.blackandwhitesensation.sice.presentation.backingBeans;

import com.blackandwhitesensation.sice.exceptions.*;
import com.blackandwhitesensation.sice.modelo.*;
import com.blackandwhitesensation.sice.modelo.dto.TipoUsuarioDTO;
import com.blackandwhitesensation.sice.presentation.businessDelegate.BusinessDelegatorView;
import com.blackandwhitesensation.sice.utilities.*;

import org.primefaces.component.calendar.*;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;

import org.primefaces.event.DateSelectEvent;
import org.primefaces.event.RowEditEvent;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;


/**
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura/
 *
 */
public class TipoUsuarioView {
    private InputText txtValorTipoUsuario;
    private InputText txtIdTipoUsuario;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<TipoUsuarioDTO> data;
    private TipoUsuarioDTO selectedTipoUsuario;

    public TipoUsuarioView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            TipoUsuarioDTO tipoUsuarioDTO = (TipoUsuarioDTO) e.getObject();

            if (txtValorTipoUsuario == null) {
                txtValorTipoUsuario = new InputText();
            }

            txtValorTipoUsuario.setValue(tipoUsuarioDTO.getValorTipoUsuario());

            if (txtIdTipoUsuario == null) {
                txtIdTipoUsuario = new InputText();
            }

            txtIdTipoUsuario.setValue(tipoUsuarioDTO.getIdTipoUsuario());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtValorTipoUsuario != null) {
            txtValorTipoUsuario.setValue(null);
            txtValorTipoUsuario.setDisabled(true);
        }

        if (txtIdTipoUsuario != null) {
            txtIdTipoUsuario.setValue(null);
            txtIdTipoUsuario.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        TipoUsuario entity = null;

        try {
            Long idTipoUsuario = new Long(txtIdTipoUsuario.getValue().toString());
            entity = BusinessDelegatorView.getTipoUsuario(idTipoUsuario);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtValorTipoUsuario.setDisabled(false);
            txtIdTipoUsuario.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtValorTipoUsuario.setValue(entity.getValorTipoUsuario());
            txtValorTipoUsuario.setDisabled(false);
            txtIdTipoUsuario.setValue(entity.getIdTipoUsuario());
            txtIdTipoUsuario.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveTipoUsuario(FacesUtils.checkLong(
                    txtIdTipoUsuario),
                FacesUtils.checkString(txtValorTipoUsuario));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteTipoUsuario(FacesUtils.checkLong(
                    txtIdTipoUsuario));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateTipoUsuario(FacesUtils.checkLong(
                    txtIdTipoUsuario),
                FacesUtils.checkString(txtValorTipoUsuario));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtIdTipoUsuario == null) {
                txtIdTipoUsuario = new InputText();
            }

            txtIdTipoUsuario.setValue(selectedTipoUsuario.getIdTipoUsuario());

            BusinessDelegatorView.deleteTipoUsuario(FacesUtils.checkLong(
                    txtIdTipoUsuario));
            data.remove(selectedTipoUsuario);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(Long idTipoUsuario,
        String valorTipoUsuario) throws Exception {
        try {
            BusinessDelegatorView.updateTipoUsuario(idTipoUsuario,
                valorTipoUsuario);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("TipoUsuarioView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtValorTipoUsuario() {
        return txtValorTipoUsuario;
    }

    public void setTxtValorTipoUsuario(InputText txtValorTipoUsuario) {
        this.txtValorTipoUsuario = txtValorTipoUsuario;
    }

    public InputText getTxtIdTipoUsuario() {
        return txtIdTipoUsuario;
    }

    public void setTxtIdTipoUsuario(InputText txtIdTipoUsuario) {
        this.txtIdTipoUsuario = txtIdTipoUsuario;
    }

    public List<TipoUsuarioDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataTipoUsuario();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<TipoUsuarioDTO> tipoUsuarioDTO) {
        this.data = tipoUsuarioDTO;
    }

    public TipoUsuarioDTO getSelectedTipoUsuario() {
        return selectedTipoUsuario;
    }

    public void setSelectedTipoUsuario(TipoUsuarioDTO tipoUsuario) {
        this.selectedTipoUsuario = tipoUsuario;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
